﻿using System.IO;
using System.Windows;
using MyFloridaHouse.libSvBingMapsMultiShape;
using MyFloridaHouse.libSvKMZ;
using MyFloridaHouse.libSvKMZ.KML;
using MyFloridaHouse.libSvOpenDistricts.Census;
using MyFloridaHouse.libSvOpenDistricts.enumerations;
using MyFloridaHouse.libSvOpenDistricts.MapShapes;
using MyFloridaHouse.libSvUtilities;

namespace MyFloridaHouse.libSvOpenDistricts.Districts
{
    public partial class DistrictPlan
    {

        #region Export

        public bool SetupFileExport(enumDistrictPlanFormatType formatType, out string filter, out string defaultExtension, out OutputFileGenerator outputFileGenerator)
        {
            bool result;
            switch (formatType)
            {
                case enumDistrictPlanFormatType.KMZ:
                    filter = "Complete District Plan (.kmz) | *.kmz";
                    defaultExtension = "kmz";
                    outputFileGenerator = ToKMZ;
                    result = true;
                    break;
                case enumDistrictPlanFormatType.BILL:
                    filter = "Bill Language (.bil) | *.bil";
                    defaultExtension = "bil";
                    outputFileGenerator = censusGeometry.ToBill;
                    result = true;
                    break;
                case enumDistrictPlanFormatType.DOJ:
                    filter = "Dept. of Justice (.doj) | *.doj";
                    defaultExtension = "doj";
                    outputFileGenerator = censusGeometry.ToDOJ;
                    result = true;
                    break;
                default:
                    MessageBox.Show("Unable to generate a file, invalid file type " + formatType.ToString());
                    outputFileGenerator = null;
                    filter = "";
                    defaultExtension = "";
                    result = false;
                    break;
            }
            return result;
        }

        public MemoryStream ToKMZ(string safeFileName)
        {
            safeFileName = safeFileName.Substring(0, safeFileName.IndexOf('.'));
            MemoryStream KMZstream = new MemoryStream();

            MemoryStream KMLstream = ToKML();

            MemoryStream DOJstream = censusGeometry.ToDOJ(safeFileName);

            KMZ.CreateKmzStream(safeFileName, KMZstream, KMLstream, DOJstream);

            return KMZstream;
        }


        private MemoryStream ToKML()
        {
            // generate the map shapes for a fully zoomed in district
            //todo: completely remove dist.districtMultiMapShape from KML input/output.  It can all be done with mapMultiShapeDTO
            foreach (var dist in districtList)
            {
                // the unassigned geography (district 0) is not written to the file
                if (dist.districtID > 0)
                {
                    if (dist.mapMultiShapeDTO != null)
                    {
                        dist.districtMultiMapShape = MapShapeTools.BuildShape(dist.districtID, MapMultiShapeTools.ReducePointsByZoomLevel(dist.mapMultiShapeDTO, MapMultiShapeTools.ReductionMaxZoomLevel));
                    }
                }
            }
            kml newKml = DistrictPlanDataContract.BuildKML(this);
            MemoryStream KMLstream = kml.Serialize(newKml);
            return KMLstream;
        }
        #endregion




        #region Import
        public void SetupFileImport(out string filter)
        {
            filter = 
                     "Complete District Plan (.kmz) | *.kmz" +
                     "|" +
                     "Dept. of Justice (.doj) | *.doj";
            return;
        }



        #endregion

    }
}
